import router from "@/router"
import AxiosMethods from "@/util/AxiosMethods"
const state = {
  user:{
    userid: null,
    username: '',
    userpassword: '',
    userimage: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
    // 以下为用户访问凭证信息，只有二者都为真时才能访问
    msg:false,
    code:0,
    confirm:"",
  },
}

const mutations = {
  // 以下为修改用户信息相关
  updateUserImage(state, name) {
    let myuser = JSON.parse(localStorage.getItem('User'))
    myuser.userimage = name
    localStorage.setItem('User', JSON.stringify(myuser))
    location.reload()
  },
  updateUsername(state, name) {
    let myuser = JSON.parse(localStorage.getItem('User'))
    myuser.username = name
    localStorage.setItem('User', JSON.stringify(myuser))
    location.reload()
  },
  updateUserPassword(state, password) {
    let myuser = JSON.parse(localStorage.getItem('User'))
    myuser.userpassword = password
    localStorage.setItem('User', JSON.stringify(myuser))
    location.reload()
  },
  // 登陆后更新用户信息，并保存到本地，确保刷新后能够保持登录状态
  UserLogin(state,data) {
    state.user.msg=true
    state.user.code=1
    state.user.confirm=data.confirm
    state.user.username=data.username
    state.user.userpassword=data.password
    // 保存登录信息到本地
    localStorage.setItem('User', JSON.stringify(state.user))
    // 登录成功后跳转到首页
    router.push('/')
  },
  // 注册后在服务器中添加用户信息，分配给用户一个uid
  UserRegister(state, data){
    state.user.username = data.username
    state.user.userpassword = data.userpassword
    // 注册成功后跳转到登录页面
    router.push('/login')
  },
}

const getters = {}

const actions = {
  // 根据uid查询用户
  async getUserByUid({commit}, uid) {
    try {
      const res = await AxiosMethods.post(`/user/select`,{uid:uid})
      if (res.data.code === 1) {
        // console.log("根据uid查询用户成功",res.data.data[0])
        return res.data.data[0]
      }
      else {
        console.log(res.data.msg)
      }
    } catch (err) {
      console.log(err)
    }
  },
  // 更新用户信息执行函数
  async updateUserInfo({commit}, data) {
    const myuser=JSON.parse(localStorage.getItem('User'))
    const parms = {
      uid:myuser.userid,
      username: data.username||myuser.username,
      password: data.userpassword||myuser.userpassword,
      avatar: data.userimage||myuser.userimage,
    }
    try {
      const res = await AxiosMethods.put(`/user`, parms)
      if (res.data.code === 1) {
        // console.log("回传的结果"+res.data.msg)
        // console.log("回传的信号"+res.data.code)
        commit('updateUsername', parms.username)
        commit('updateUserPassword', parms.password)
        commit('updateUserImage', parms.avatar)
      } else {
        console.log(res.data.msg)
      }
    } catch (err) {
      console.log(err)
    }
  },

  // 注销执行函数
  async deletethisuser(state) {
    const uid=JSON.parse(localStorage.getItem('User')).userid
    console.log('注销用户'+uid)
    try{
      const res = await AxiosMethods.delete(`/user/${uid}`)
      if(res.data.code === 1){
        localStorage.removeItem('User')
        location.reload()
      }else{
        this.$message.error(res.data.msg)
      }
    }catch(err){
      console.log(err)
    }
  },
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}